#!/bin/bash
#THIS SCRIPT WRITTEN FOR CARB TO BACK POLL DATA FROM 
#CARBLOGGER BY POSTING THE DATE RANGES NEEDED FOR THE BACKPOLL
#TO A PUBLIC WEBSITE. At present it should be run periodically by CRON.
#PUBLIC DISPATCH FILE LOCATION
hour=`date '+%H'`
error_file="Data/errors${hour}"
public="http://arb.ca.gov/airwebmanual/.carbcallhome.txt"
file=`basename ${public}`
rm $file
#FUNCTION TO GET THE CURRENT CARBLOGGER CONFIGURATION PARAMETERS
read_config(){
	
	while read line
	do
		eval ${line}
	done
	}

#RUN THE FUNCTION AGAINST THE CONFIG FILE	
read_config<Config/config

#FUNCTION TO GET THE PUBLIC DISPATCH
get_dispatch(){
	if !(wget ${public}); then
		{
		echo "CARBLogger had a problem grabbing the dispatch file from the public webserver....exiting">>${error_file}
		#cat $error_file
		exit 1
		}
	fi
	wait $!
	}
#FUNCTION TO FIND AN NEW COMMAND THAT ARE PERTINENT TO THIS SITE
discern(){
		#cleanup old processing files
		rm .toprocess 2> /dev/null;
		rm .tmppast 2> /dev/null;
		rm .tmpcmds 2> /dev/null;
		#CREATE A SORTED FILE CONTAINING ALL PREVIOUS COMMANDS
		sort "sys_resources/pastactions.txt" | sed 's/\\//g' >.tmppast
		#CREATE A FILE OF ALL REQUESTS APPLICABLE TO THIS SITE
		#CURRENTLY ADVERTISED ON CARB WEBSITE
		sort ".carbcallhome.txt" | sed 's/\\//g' | sed 's/^$//g' | grep $saroad >.tmpcmds
		sort ".carbcallhome.txt" | sed 's/\\//g' | sed 's/^$//g' | grep ALL >> .tmpcmds
		#SORT THE ABOVE FILE IN PREPARATION FOR THE COMM COMMAND
		sort ".tmpcmds" > .tmpstuff
		mv .tmpstuff .tmpcmds
		
		comm -2 -3 .tmpcmds .tmppast > .tmpwrkcue 
		cat .tmpwrkcue | grep -v ^$ |\
		while read line 
		do
		dist=`echo $line | awk '{print $2}'`
		date1=`echo $line | awk '{ print $3 " " $4 }'`
		date2=`echo $line | awk '{ print $5 " " $6 }'`
		#CHECK TO SEE THE CORRCT NUMBER OF VARIABLES HAS BEEN PASSED
		columns=`echo $line | wc -w`
			if [ $columns -ne 6 ]; then
			{
				echo "number of columns= $columns"
				exit
			}
			else
			{
		
				echo "dist = ${dist}"
				echo "Date 1 is ${date1}"
				d1=`date -d "$date1"`
				echo $d1
				echo "Date 2 is ${date2}"
				d2=`date -d "$date2"`
				echo $d2
				#REJECT ANY REQUESTS THAT WANT MORE THAN 744 HOURS OF DATA
				total_hours=`echo "($(date --date="$date2" +%s) - $(date --date="$date1" +%s))/(60*60)" | bc`
				if [ ${total_hours} -ge 744 ]; then
				{
					echo ${line} >> sys_resources/pastactions.txt
					echo "${line} was not processed because it asked for more than 744 hours worth of data (31 days)" >> ${error_file}
					echo "${line} was not processed because it asked for more than 744 hours worth of data (31 days)"
				}
				elif [ ${total_hours} -lt 0 ]; then
				{
					echo ${line} >> sys_resources/pastactions.txt
					echo "${line} was not processed because it asked for a negative number of hours" >> ${error_file}
					echo "${line} was not processed because it asked for a negative number of hours"
				}
				else
				{
				#PROCESS AND REQUESTS THAT STILL HAVE TO BE ADDRESSED
					echo "AM PROCESSING ${line}"
					echo "Backpoll Begining date ${date1} and ending date ${date2}"
					echo "${line}" >> sys_resources/pastactions.txt
					bash sys_resources/dmsbackpollout.sh "${date1}" "${date2}" 1>/dev/null 2> ${error_file}
					
				}
				fi
			}
			fi
		done
		
	}	
	
	#DEBUG
	echo ${saroad}
	get_dispatch # 2> ${error_file}&# 1> /dev/null&
	discern
	wait $!
	cp sys_resources/*.MIN .
	rm -f sys_resources/*.MIN
	#rm .tmp*
	cat $file

exit
